﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.Base;
using QLBH.DataModel;
using QLBH.Model.Controller;

namespace QLBH.Category
{
    public partial class AddNhaCungCapDlg : Form
    {
        public eDlgType DlgType { set; get; }
        public NHACUNGCAP Model { set; get; }
        public AddNhaCungCapDlg()
        {
            InitializeComponent();

            Model = new NHACUNGCAP();
            Model.NCCUID = Guid.NewGuid();
            Model.NCCCODE = CodeHelper.GetNewCode();

            Model.NGAYTAO = DateTime.Now;            
        }

        private void AddNhaCungCapDlg_Load(object sender, EventArgs e)
        {
            if (DlgType == eDlgType.ADD)
            {
                this.Text = "Thêm mới nhà cung cấp";

                UpdateModelToControl();
            }
            else
            {
                this.Text = "Sửa thông tin nhà cung cấp";

                UpdateModelToControl();

                dtpNgayTao.Enabled = false;
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void AddNhaCungCapDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            else if (e.Control && e.KeyCode == Keys.S)
            {
                //Ctrl+S
                btnSave_Click(sender, e);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckValidData())
            {
                try
                {
                    UpdateModelFromControl();
                    if (DlgType == eDlgType.ADD)
                    {
                        Model = NCCController.GetInstance().Add(Model);
                    }
                    else
                    {
                        NCCController.GetInstance().Edit(Model);
                    }
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "QL Nhà cung cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        private void UpdateModelToControl()
        {
            tbxMaNCC.Text = Model.NCCCODE;
            tbxTenNCC.Text = Model.TENNCC;
            tbxNguoiDaiDien.Text = Model.NGUOIDAIDIEN;
            tbxChuVu.Text = Model.CHUCVU;

            tbxMaSoThue.Text = Model.MASOTHUE;
            tbxDiDong.Text = Model.DIDONG;
            tbxDienThoai.Text = Model.DIENTHOAI;
            tbxEmail.Text = Model.EMAIL;

            tbxFax.Text = Model.FAX;
            rtbGhiChu.Text = Model.GHICHU;
            rtbDiaChi.Text = Model.DIACHI;
            tbxwebsite.Text = Model.WEBSITE;
            dtpNgayTao.Value = (DateTime)Model.NGAYTAO;
        }
        private void UpdateModelFromControl()
        {
            Model.NCCCODE = tbxMaNCC.Text;
            Model.TENNCC = tbxTenNCC.Text;
            Model.MASOTHUE = tbxMaSoThue.Text;
            Model.NGAYTAO = dtpNgayTao.Value;
            Model.CHUCVU = tbxChuVu.Text;

            Model.NGUOIDAIDIEN = tbxNguoiDaiDien.Text;
            Model.DIACHI = rtbDiaChi.Text;
            Model.DIDONG = tbxDiDong.Text;
            Model.EMAIL = tbxEmail.Text;

            Model.GHICHU = rtbGhiChu.Text;
            Model.FAX = tbxFax.Text;
            Model.WEBSITE = tbxwebsite.Text;
            Model.DIENTHOAI = tbxDienThoai.Text;
        }
        private bool CheckValidData()
        {
            try
            {
                DataInputHelper.CheckTextData(tbxMaNCC.Text, "Mã NCC", 14);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhà cung cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxMaNCC.SelectAll();
                tbxMaNCC.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(tbxTenNCC.Text, "Tên NCC", 50);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhà cung cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxTenNCC.SelectAll();
                tbxTenNCC.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(tbxNguoiDaiDien.Text, "Tên người đại diện", 50);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhà cung cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxNguoiDaiDien.SelectAll();
                tbxNguoiDaiDien.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckEmailData(tbxEmail.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhà cung cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxEmail.SelectAll();
                tbxEmail.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckPhoneNumber(tbxDienThoai.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhà cung cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxDienThoai.SelectAll();
                tbxDienThoai.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckPhoneNumber(tbxDiDong.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhà cung cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxDiDong.SelectAll();
                tbxDiDong.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckFaxNumber(tbxFax.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhà cung cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxFax.SelectAll();
                tbxFax.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckDateTime((DateTime)dtpNgayTao.Value, "Ngày tạo");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhà cung cấp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtpNgayTao.Select();
                dtpNgayTao.Focus();
                return false;
            }           
            return true;
        }
    }
}
